package dao.daocontrollers;

import java.util.ArrayList;
import java.util.Date;

import workerswithdb.IDAOGarage;
import workerswithdb.IDAOMaster;
import workerswithdb.IDAOOrder;
import workerswithdb.IDAOWorker;
import workerswithdb.SortMastersBy;
import workerswithdb.SortOrdersBy;
import api.entitites.IGarage;
import api.entitites.IMaster;
import api.entitites.IOrder;
import api.entitites.OrderStatus;
import api.exportimportentities.IExportImportEntities;
import api.messages.Messager;
import autoproperties.AutoProperties;
import entitiesfactory.EntitiesFactory;

public class DAOWorker implements IDAOWorker{
	private IDAOGarage daoGarage;
	private IDAOOrder daoOrder;
	private IDAOMaster daoMaster;
	private EntitiesFactory entitiesFactory=EntitiesFactory.getInstance();
	private AutoProperties autoProperties;
	private ArrayList<String> exceptionMap = new ArrayList<String>();
	private IExportImportEntities export_impot_entities;

	public DAOWorker() {
		daoGarage = new DAOGarage();
		daoOrder = new DAOOrder();
		daoMaster = new DAOMaster();
		autoProperties = AutoProperties.getInstance();
		export_impot_entities = (IExportImportEntities) entitiesFactory
				.getObject(IExportImportEntities.class);
	}

	public ArrayList<String> getEmptyGarage() {
		return daoGarage.getEmptyGarage();
	}

	public ArrayList<String> getOrdersByMaster(String masterName) {
		return daoMaster.getOrderByMaster(masterName);
	}

	public ArrayList<String> getMastersByOrder(Integer numberOrder) {
		return daoOrder.getMasterByOrder(numberOrder);
	}

	public ArrayList<String> getEmptyPlacesOnDate(Date date) {
		return daoGarage.getEmptyPlacesOnDate(date);
	}

	public ArrayList<String> getNextFreeDate() {
		return daoOrder.getNextFreeDate();
	}

	public ArrayList<String> SortOrderByBeginningDate() {
		return daoOrder.sortAllOrders(SortOrdersBy.begining_date);
	}

	public ArrayList<String> SortOrderByDeadlineDate() {
		return daoOrder.sortAllOrders(SortOrdersBy.deadline_date);
	}

	public ArrayList<String> SortOrderByFillingDate() {
		return daoOrder.sortAllOrders(SortOrdersBy.filling_date);
	}

	public ArrayList<String> SortOrderByPrice() {
		return daoOrder.sortAllOrders(SortOrdersBy.price);
	}

	public ArrayList<String> SortMasterByEmployment() {
		return daoMaster.SortMasters(SortMastersBy.employment_master);
	}

	public ArrayList<String> SortMasterByName() {
		return daoMaster.SortMasters(SortMastersBy.master_name);
	}

	public ArrayList<String> SortMadingOrderByBeginningDate() {
		return daoOrder.sortMadingOrders(SortOrdersBy.begining_date);
	}

	public ArrayList<String> SortMadingOrderByDeadlineDate() {
		return daoOrder.sortMadingOrders(SortOrdersBy.deadline_date);
	}

	public ArrayList<String> SortMadingOrderByFillinggDate() {
		return daoOrder.sortMadingOrders(SortOrdersBy.filling_date);
	}

	public ArrayList<String> SortMadingOrderByPrice() {
		return daoOrder.sortMadingOrders(SortOrdersBy.price);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.begining_date, OrderStatus.DELETED);
	}

	public ArrayList<String> SortCanceledOrderByPeriodByBeginningDate(
			Date begin, Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.begining_date, OrderStatus.CANCELED);
	}

	public ArrayList<String> SortMadeOrderByPeriodByBeginningDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.begining_date, OrderStatus.MADE);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.deadline_date, OrderStatus.DELETED);
	}

	public ArrayList<String> SortCanceledOrderByPeriodByDeadlineDate(
			Date begin, Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.deadline_date, OrderStatus.CANCELED);
	}

	public ArrayList<String> SortMadeOrderByPeriodByDeadlineDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.deadline_date, OrderStatus.MADE);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByFillingDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.filling_date, OrderStatus.DELETED);
	}

	public ArrayList<String> SortCanceledOrderByPeriodByFillingDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.filling_date, OrderStatus.CANCELED);
	}

	public ArrayList<String> SortMadeOrderByPeriodByFillingDate(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end,
				SortOrdersBy.filling_date, OrderStatus.MADE);
	}

	public ArrayList<String> SortDeletedOrderByPeriodByPrice(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end, SortOrdersBy.price,
				OrderStatus.DELETED);
	}

	public ArrayList<String> SortCanceledOrderByPeriodByPrice(Date begin,
			Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end, SortOrdersBy.price,
				OrderStatus.CANCELED);
	}

	public ArrayList<String> SortMadeOrderByPeriodByPrice(Date begin, Date end) {
		return daoOrder.sortOrdersByPeriod(begin, end, SortOrdersBy.price,
				OrderStatus.MADE);
	}

	public ArrayList<String> addMaster(String masterName) {

		IMaster master = (IMaster) entitiesFactory.getObject(IMaster.class);
		master.setNameMaster(masterName);
		return daoMaster.insertMasterToDB(master);

	}

	public ArrayList<String> deleteMaster(String masterName) {
		return daoMaster.deleteMaster(masterName);

	}

	public ArrayList<String> addGarage(Integer garageNumber) {
		if (autoProperties.getGarageManager() == true) {
			IGarage garage = (IGarage) entitiesFactory.getObject(IGarage.class);
			garage.setNumberGarage(garageNumber);
			return daoGarage.insertGarageToDB(garage);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return exceptionMap;
		}

	}

	public ArrayList<String> deleteGarage(Integer numberGarage) {
		if (autoProperties.getGarageManager()) {
			return daoGarage.deleteGarage(numberGarage);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return exceptionMap;
		}
	}

	public ArrayList<String> addOrder(Integer orderNumber, Date fillingDate,
			Date deadlineDate, Date beginingDate, Integer price,
			String masterName, Integer numberGarage) {
		IOrder order = (IOrder) entitiesFactory.getObject(IOrder.class);
		order.setNumberOrder(orderNumber);
		order.setFillingDate(fillingDate);
		order.setDeadlineDate(deadlineDate);
		order.setBeginingDate(beginingDate);
		order.setPrice(price);
		Integer id_master = daoMaster.getIdMaster(masterName);
		Integer id_garage = daoGarage.getIdGarage(numberGarage);
		order.sendMasterGarage(id_master, id_garage);
		return daoOrder.insertOrderToDB(order);

	}

	public ArrayList<String> markOrderDeleted(Integer numberOrder) {
		if (autoProperties.getPermissionDeleteOrder() == true) {
			return daoOrder.markOrder(numberOrder, OrderStatus.DELETED);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return exceptionMap;

		}
	}

	public ArrayList<String> markOrderCanceled(Integer numberOrder) {
		return daoOrder.markOrder(numberOrder, OrderStatus.CANCELED);
	}

	public ArrayList<String> markOrderMade(Integer numberOrder) {
		return daoOrder.markOrder(numberOrder, OrderStatus.MADE);
	}

	public ArrayList<String> ShiftTimeOrder(Date newDate, Integer numberOrder) {
		if (autoProperties.getPermissionShiftTimeOrder()) {
			return daoOrder.shiftDeadlineDate(newDate, numberOrder);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.THIS_ACTION_NOT_ALLOWED);
			return exceptionMap;
		}
	}

	public ArrayList<String> cloneOrder(Integer numberOrder) {
		return daoOrder.cloneOrder(numberOrder);
	}

	public ArrayList<String> exportGarages(Integer numberGarage) {
		IGarage garage = daoGarage.getGarageFromDb(numberGarage);
		if (garage != null) {
			return export_impot_entities.ExportGarages(garage);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.GARAGE_NOT_EXISTS);
			return exceptionMap;
		}
	}

	public ArrayList<String> exportMasters(String name_master) {
		IMaster master = daoMaster.getMasterFromDb(name_master);
		if (master != null) {
			return export_impot_entities.ExportMasters(master);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.MAS_NOT_EXISTS);
			return exceptionMap;

		}
	}

	public ArrayList<String> exportOrders(Integer number_order) {
		IOrder order = daoOrder.getOrderFromDb(number_order);
		if (order != null) {
			return export_impot_entities.ExportOrders(order);
		} else {
			exceptionMap.clear();
			exceptionMap.add(Messager.ORD_NOT_EXISTS);
			return exceptionMap;

		}
	}

	public ArrayList<String> importGarages() {
		try {
			for (IGarage gar : export_impot_entities.ImportGarages()) {
				exceptionMap.clear();
				exceptionMap.addAll(daoGarage.insertGarageToDB(gar));
			}
			return exceptionMap;
		} catch (Exception e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.ERROR);
			return exceptionMap;
		}

	}

	public ArrayList<String> importMasters() {
		try {
			for (IMaster mas : export_impot_entities.ImportMasters()) {
				exceptionMap.clear();
				exceptionMap.addAll(daoMaster.insertMasterToDB(mas));
			}
			return exceptionMap;
		} catch (Exception e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.ERROR);
			return exceptionMap;
		}

	}

	public ArrayList<String> importOrders() {
		try {
			for (IOrder ord : export_impot_entities.ImportOrders()) {
				exceptionMap.clear();
				exceptionMap.addAll(daoOrder.insertOrderToDB(ord));
			}
			return exceptionMap;
		} catch (Exception e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.ERROR);
			return exceptionMap;
		}
	}

}
